package site.yunnong.atvris.admin.controller;

import io.swagger.annotations.Api;
import io.swagger.annotations.ApiImplicitParam;
import io.swagger.annotations.ApiImplicitParams;
import io.swagger.annotations.ApiOperation;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.util.CollectionUtils;
import org.springframework.web.bind.annotation.*;
import site.yunnong.atvris.dao.common.api.CommonPage;
import site.yunnong.atvris.dao.common.api.CommonResult;
import site.yunnong.atvris.dao.common.api.YunnongHttpStatus;
import site.yunnong.atvris.dao.common.util.LocalAssert;
import site.yunnong.atvris.dao.portal.service.VideoService;
import site.yunnong.atvris.dao.portal.vo.SelfVideoCardResult;
import site.yunnong.atvris.dao.portal.vo.VideoPostParam;

import javax.validation.Valid;
import java.util.List;

/**
 * @author JinghaoZhang
 * @date 2021年-04月-07日
 */
@Slf4j
@Api(tags = "VideoManageController", description = "个人视频管理")
@RestController
@RequestMapping("/video")
public class VideoManageController {

    @Autowired
    private VideoService videoService;

    @ApiOperation("上传视频")
    @PostMapping
    public CommonResult saveVideo(@Valid @RequestBody VideoPostParam upload) {
        int count = videoService.save(upload);
        LocalAssert.notZero(count, "上传失败");
        return CommonResult.success();
    }

    @ApiOperation("删除视频")
    @DeleteMapping("/{videoId}")
    public CommonResult getVideo(@PathVariable Long videoId) {
        int count = videoService.delete(videoId);
        LocalAssert.notZero(count, "无此视频");
        return CommonResult.result(YunnongHttpStatus.SUCCESS);
    }

    @ApiOperation("分页获取自己上传过的视频")
    @ApiImplicitParams({
            @ApiImplicitParam(name = "status", value = "-1：未通过审核，0：审核中，1：通过审核, null：全部（默认）"),
            @ApiImplicitParam(name = "order",
                    value = "0->按投稿时间排序（默认），1->按播放量排序，2->按点赞量排序，3->按评论数排序，4->按收藏数排序"
                    , defaultValue = "0"),
            @ApiImplicitParam(name = "partition", value = "根据分区获取视频")
    })
    @GetMapping("/list")
    public CommonResult<CommonPage<SelfVideoCardResult>> getVideoList(@RequestParam(value = "pageSize", defaultValue = "4") Integer pageSize,
                                                                      @RequestParam(value = "pageNum", defaultValue = "1") Integer pageNum,
                                                                      @RequestParam(value = "status", required = false) Integer status,
                                                                      @RequestParam(value = "order", defaultValue = "0") Integer order,
                                                                      @RequestParam(value = "partitionId", required = false) Long partitionId) {
        List<SelfVideoCardResult> videoCards = videoService.list(pageNum, pageSize, status, order, partitionId);
        if (CollectionUtils.isEmpty(videoCards)) {
            // 如果没有的话需要特别处理
            return CommonResult.result(YunnongHttpStatus.NO_CONTENT);
        }
        Long count = videoService.count(status, partitionId);
        CommonPage<SelfVideoCardResult> commonPage = CommonPage.selfPage(pageNum, pageSize, videoCards, count);
        return CommonResult.result(commonPage, YunnongHttpStatus.SUCCESS);
    }

    @ApiOperation("编辑视频")
    @PostMapping("/edit")
    public CommonResult editVideo(@Valid @RequestBody VideoPostParam upload) {
        int count = videoService.update(upload);
        LocalAssert.notZero(count, "编辑视频失败");
        return CommonResult.result(YunnongHttpStatus.NO_CONTENT);
    }
}
